package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.crypto.Cipher;
import java.util.Date;

@Service
@Slf4j
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {
        //判断是账号密码登录还是设备id登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user != null){
            //根据用户id,查询对应的实体
            ApBehaviorEntry ap = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
            //根据实体id,文章Id增加阅读行为
            NumberOfArticleReads(ap.getId(),dto);
            //返回响应
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            if (dto.getEquipmentId() != null){
                //根据用户id,查询对应的实体
                ApBehaviorEntry ap = apBehaviorEntryService.findByUserIdOrEquipmentId(null, dto.getEquipmentId());
                //根据实体id,文章Id增加阅读行为
                NumberOfArticleReads(ap.getId(),dto);
                //返回响应
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }else{
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "当前用户未登录或者设备id不存在");
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
    }


    private void NumberOfArticleReads(String id, ReadBehaviorDTO dto) {
        //根据实体id,文章id判断是否阅读过
        Query query = Query.query(Criteria.where("entryId").is(id).and("articleId").is(dto.getArticleId()));
        ApReadBehavior one = mongoTemplate.findOne(query, ApReadBehavior.class);
        //判断阅读记录是否存在
        if (one != null){
            //在原先的阅读记录上+1,更新更新时间
            one.setCount((short) (one.getCount()+1));
            one.setUpdatedTime(new Date());
            mongoTemplate.save(one);
        }else{
            //创建新的记录,设置登录时间,设置阅读记录为1
            ApReadBehavior apReadBehavior = new ApReadBehavior();
            apReadBehavior.setCount((short)1);//初始阅读记录1
            apReadBehavior.setArticleId(dto.getArticleId());
            apReadBehavior.setEntryId(id);
            apReadBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apReadBehavior);
        }
    }
}
